package com.by;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
public class StringZiFuZhongJianJia {
	@Test
	public void t1() {
		int[] x = {2, 4, 1, 7, 6};
		ArrayList a = new ArrayList();
		for (int i = 0; i < x.length; i++) {
			a.add(x[i]);
		}
		String qq = "";
		for (Object aa : a) {
			qq = qq + aa;
			if (qq.length() < a.size() * 2 - 1) {
				qq = qq + ",";
			}
		}
		System.out.println(qq);
	}

	@Test
	public void t2() {
		int[] x = {2, 4, 1, 7, 6, 2, 5, 6, 9, 4, 1, 2};
		StringBuffer b = new StringBuffer();
		for (int i = 0; i < x.length; i++) {
			b.append(x[i]);
			if (b.length()<x.length*2-1){
				b.append(",");
			}
		}
		String q = b.toString();
		System.out.println(q);
	//	System.out.println(displayWithComma(q, 3));
	}

	public  String displayWithComma(String str, int index) {
		str = new StringBuffer(str).toString(); // 字符串不颠倒顺序   颠倒的意义在于从右向左开始分割追加     不颠倒是从左向右
		String str2 = "";
		int size = (str.length() % index == 0) ? (str.length() / index) : (str.length() / index + 1);
		for (int i = 0; i < size - 1; i++) {
			str2 += str.substring(i * index, i * index + index) + ",";
		}
		for (int i = size - 1; i < size; i++) {
			str2 += str.substring(i * index, str.length());
		}
		str2 = new StringBuffer(str2).toString();
		return str2;
	}

	/**
	 * 传参后进行从右向左追加
	 * @param str
	 * @param index
	 * @param res
	 * @return
	 */
	public  String displayWithComma(String str, int index ,int res) {
		str = new StringBuffer(str).reverse().toString(); // 先将字符串颠倒顺序   颠倒的意义在于从右向左开始分割追加     不颠倒是从左向右
		String str2 = "";
		int size = (str.length() % index == 0) ? (str.length() / index) : (str.length() / index + 1);
		for (int i = 0; i < size - 1; i++) {
			str2 += str.substring(i * index, i * index + index) + ",";
		}
		for (int i = size - 1; i < size; i++) {
			str2 += str.substring(i * index, str.length());
		}
		str2 = new StringBuffer(str2).reverse().toString();
		return str2;
	}
}

